using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyTank : TankBase
{
    //当前目标点
    private Transform targetPos;
    public Transform[] randomPos;
    public Transform Player;
    public float fireOffsetTime = 1f;
    private float nowTime;

    public float FireDistance = 5;
    public Texture maxHpBk;
    public Texture hpBk;
    //Rect是结构体，可以不用实例化
    private Rect maxHpRec;
    private Rect hpRec;
    private float guiShowtime;
    // Start is called before the first frame update
    void Start()
    {
        RandomPos();
    }

    // Update is called once per frame
    void Update()
    {


        if (Vector3.Distance(transform.position, targetPos.position) < 0.05)
        {
            RandomPos();
        }
        transform.LookAt(targetPos);
        transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);

        if (Player != null)
        {
            if (Vector3.Distance(transform.position, Player.position) <= FireDistance)
            {
                head.transform.LookAt(Player);
                nowTime += Time.deltaTime;
                if (nowTime > fireOffsetTime)
                {
                    Fire();
                    nowTime = 0;
                }
            }

        }

    }
    private void RandomPos()
    {
        if (randomPos.Length == 0)
        {
            Debug.Log("randomPos为空");
            return;
        }

        targetPos = randomPos[Random.Range(0, 2)];
    }
    void OnTriggerEnter(Collider other)
    {
        if (other.tag == "PlayerBullet")
        {
            BulletObject bullet = other.GetComponent<BulletObject>();
            //更新血量
            Wound(bullet.fatherObject);
        }
    }
    public override void DeadEffect()
    {
        //敌人死亡加分
        GamePanel.Instance.AddScore(10);
        //死亡特效
        base.DeadEffect();
    }
    public override void Wound(TankBase other)
    {
        //设置血条计时
        guiShowtime = 3f;
        base.Wound(other);
    }

    private void OnGUI()
    {
        if (guiShowtime > 0)
        {
            //显示倒计时
            guiShowtime -= Time.deltaTime;
            //将当前物体的坐标转换成主摄像机的屏幕坐标
            Vector3 screenPos = Camera.main.WorldToScreenPoint(transform.position);
            //屏幕坐标转换成GUI位置
            screenPos.y = Screen.height - screenPos.y;
            //GUI位置偏移，让GUI偏移到物体上方
            screenPos.x -= 50f;
            screenPos.y -= 70f;
            //绘制GUI
            maxHpRec.x = screenPos.x;
            maxHpRec.y = screenPos.y;
            maxHpRec.width = 100f;
            maxHpRec.height = 15f;
            GUI.DrawTexture(maxHpRec, maxHpBk);

            hpRec.x = screenPos.x;
            hpRec.y = screenPos.y;
            hpRec.width = hp / originalmaxHp * maxHpRec.width;
            hpRec.height = maxHpRec.height;
            GUI.DrawTexture(hpRec, hpBk);
        }
    }
}
